class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int l = 0;
        int cnt = 0;
        int n = nums.size();
        int ma = 0;
        for (int r = 0; r < n; r++) {
            if (!nums[r])cnt++;
            while (cnt > k) {
                if (!nums[l])cnt--;
                l++;
            }
            ma = max(ma, r - l + 1);
        }
        return ma; 
    }
};